echo off
setlocal

:: 设置 Django 项目的路径
set PROJECT_PATH=/Volumes/mobile/code/mine/guanxian/ht_guangxian/

:: 设置 Django 管理命令的路径
set MANAGE_PATH=%PROJECT_PATH%\manage.py

:: 设置日志文件路径
set LOG_FILE=%PROJECT_PATH%\logs\django.log

:: 设置虚拟环境路径（如果有）
set VENV_PATH=%PROJECT_PATH%\venv_gx\bin\activate

:: 检查命令参数
if "%1"=="" (
    echo Usage: manage_django.bat [start|stop|restart]
    exit /b 1
)

:: 启动 Django 服务
if "%1"=="start" (
    if exist "%VENV_PATH%" (
        call "%VENV_PATH%"
    )
    cd /d "%PROJECT_PATH%"
    python "%MANAGE_PATH%" runserver 0.0.0.0:9000 > "%LOG_FILE%" 2>&1
    echo Django server started. Logs are written to %LOG_FILE%
    exit /b 0
)

:: 停止 Django 服务
if "%1"=="stop" (
    taskkill /F /IM python.exe /FI "MODULES eq django.core.management"
    echo Django server stopped.
    exit /b 0
)

:: 重启 Django 服务
if "%1"=="restart" (
    call :stop
    call :start
    exit /b 0
)

:: 默认情况
echo Invalid command. Use start, stop, or restart.
exit /b 1

:end
endlocal